// miniprogram/pages/word/list/list.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    words: null,
    buttonText: '少女祈祷中',
    number: 0,
  },

  /**
   * 点击进行测试
   */
  navigateToWordRemember(e) {
    let app = getApp()
    let thisWordId = app.globalData.lastWordId + 1
    let total = wx.getStorageSync("batch")
    let current = app.globalData.studiedToday + 1
    if (current > total) { //背诵完毕了
      wx.navigateTo({
        url: '/pages/word/end/end',
      })
      return
    }
    wx.navigateTo({
      url: '/pages/word/wordRemember/wordRemember?wordId=' + thisWordId +
        "&total=" + total +
        "&current=" + current,
    });
  },

  /**
   * 从数据库获取本日要背诵的数据
   */
  getWordList(e) {
    const thiz = this
    //获取words
    thiz.setData({
      loadModal: true
    })
    wx.cloud.callFunction({
      // 要调用的云函数名称
      name: 'wordsToday',
      // 传递给云函数的参数
      data: {
        num: e
      },
      success: res => {
        // output: res.result === 3
        thiz.setData({
          words: res.result.words
        })
        thiz.setButtonText(thiz)
      },
      fail: err => {
        // handle error
      },
      complete: () => {
        // ...
        thiz.setData({
          loadModal: false
        })
      }
    })

  },
  /**
   * 根据现状设置按钮文字
   */
  setButtonText(context) {
    const app = getApp()
    let thisWordId = app.globalData.lastWordId + 1
    console.log("thisWordId", thisWordId)
    let firstWordId = context.data.words[0].word_id
    let endWordId = context.data.words[context.data.words.length - 1].word_id
    if (thisWordId == firstWordId) {
      context.setData({
        buttonText: "开始测试"
      })
    } else if (thisWordId > firstWordId && thisWordId <= endWordId) {
      context.setData({
        buttonText: "继续测试"
      })
    } else if (thisWordId > endWordId) {
      context.setData({
        buttonText: "目标完成"
      })
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    const thiz = this
    let number = Number(options.number)
    //获取本次所有数据
    this.getWordList(number)
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    if (this.data.words != null) {
      this.setButtonText(this)
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})